﻿using System;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Attributes;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Skip]
    public class LanJianAwardsController : BaseController
    {
        #region 列表
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                var direction = Request["order[0][dir]"];
                var orderByColumns = new[] { "AwardName", "AwardImg", "TotalQuantity", "SendQuantity", "IsOpen" };
                var search = Request["search[value]"];
                var list = LanJianAwardsBLL.Instance.GetPagingList(draw, (start / length) + 1, length, SqlSafe(search), orderByColumns[order] + " " + direction);
                return Json(list);
            }
            catch (Exception ex)
            {
                Logger.Error("LanJianAwards Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }
        #endregion

        #region 创建
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection, LanJianAwards model)
        {
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var strUploadImage = UploadImage("fileImage", "/Awards/LanJianAwards/", 300, 300);
            if (!string.IsNullOrEmpty(strUploadImage))
            {
                model.AwardImg = strUploadImage;
            }
            model.AddTime = DateTime.Now;
            var newId = LanJianAwardsBLL.Instance.Add(model);
            if (newId > 0)
            {
                return RedirectToAction("List");
            }
            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 修改
        public ActionResult Update(int id)
        {
            if (id <= 0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var model = LanJianAwardsBLL.Instance.GetModel(id);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }
            ViewBag.theHeadPic = ConfigurationManager.AppSettings["ImgFileSitePath"] + model.AwardImg;
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(LanJianAwards model)
        {
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var tempModel = LanJianAwardsBLL.Instance.GetModel(model.Id);
            tempModel.AwardName = model.AwardName;
            tempModel.JumpUrl = model.JumpUrl;
            var strUploadImage = UploadImage("fileImage", "/Awards/LanJianAwards/", 300, 300);
            if (!string.IsNullOrEmpty(strUploadImage))
            {
                tempModel.AwardImg = strUploadImage;
            }
            tempModel.TotalQuantity = model.TotalQuantity;
            tempModel.IsOpen = model.IsOpen;
            if (LanJianAwardsBLL.Instance.Update(tempModel))
            {
                return RedirectToAction("List");
            }
            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Delete()
        {
            var result = LanJianAwardsBLL.Instance.DeleteList(Request["doIds"]);
            return Json(result);
        }
        #endregion
    }
}
